class Solution:
    def sortByBits(self, arr: List[int]) -> List[int]:
        arr.sort()
        def countbit(num):
            count=0
            while num>0:
                count+=1
                num=num&(num-1)
            return count
        return sorted(arr,key=lambda x:countbit(x))